<?php

namespace App\Domain\Customer\Requests;

use App\Domain\Customer\Services\CustomerService;
use Illuminate\Foundation\Http\FormRequest;

class UpdateCustomerPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'oldPassword' => ["required", function ($attribute, $value, $fail) {
                if ($value != '-1') {
                    $password = (new CustomerService())->findPassword($value);
                    if (!$password) {
                        $fail("旧密码错误");
                    }
                }
            }],
            'newPassword' => 'required|min:8|max:16|different:oldPassword'
        ];
    }

    public function messages()
    {
        return [
            'oldPassword.required' => '旧密码必填',
            'oldPassword.min' => '密码最少8位',
            'oldPassword.max' => '密码最多16位',
            'newPassword.required' => '新密码必填',
            'newPassword.min' => '密码最少8位',
            'newPassword.max' => '密码最多16位',
            'newPassword.different' => '新密码和旧密码不能相同',
        ];
    }
}
